/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.rrhh.controlador;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sv.com.intesal.clases.abstracts.ControladorAbstract;
import sv.com.intesal.rrhh.bean.Area;
import sv.com.intesal.rrhh.bean.Cargo;
import sv.com.intesal.rrhh.sql.SQLCargo;

/**
 *
 * @author ejerez
 */
public class CargoControlador extends ControladorAbstract {

    public void insert(Cargo c) throws Exception {
        try {
            super.setPreparedStatement(SQLCargo.insert);
            super.ps.setInt(1, c.getCodigo_a());
            super.ps.setString(2, c.getNombre_c());
            super.ps.setString(3, super.v.getEstado(c.getEstado_filtro_c()));
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public void update(Cargo c) throws Exception {
        try {
            super.setPreparedStatement(SQLCargo.update);
            super.ps.setInt(1, c.getCodigo_a());
            super.ps.setString(2, c.getNombre_c());
            super.ps.setString(3, super.v.getEstado(c.getEstado_filtro_c()));
            super.ps.setInt(4, c.getCodigo_c());
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Cargo> getCargos() throws Exception {
        List<Cargo> lista = new ArrayList<Cargo>();
        try {
            this.getExecuteQuery(SQLCargo.getCargos);
            while (super.rs.next()) {
                lista.add(this.getCargo(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Cargo> getCargoActivos() throws Exception {
        List<Cargo> lista = new ArrayList<Cargo>();
        try {
            this.getExecuteQuery(SQLCargo.getCargoActivas);
            while (super.rs.next()) {
                lista.add(this.getCargo(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Cargo> getCargoActivos_x_Area(Integer codigo_a) throws Exception {
        List<Cargo> lista = new ArrayList<Cargo>();
        try {
            super.setPreparedStatement(SQLCargo.getCargoActivas_x_Area);
            super.ps.setInt(1, codigo_a);
            super.rs = super.ps.executeQuery();
            while (super.rs.next()) {
                lista.add(this.getCargo(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    private Cargo getCargo(ResultSet rs) throws Exception {
        try {
            Cargo c = new Cargo();
            c.setCodigo_c(rs.getInt("codigo_c"));
            c.setNombre_c(rs.getString("nombre_c"));
            c.setEstado_c(rs.getString("estado_c"));
            c.setEstado_filtro_c(v.getEstado(rs.getString("estado_c")));

            c.setNombre_a(rs.getString("nombre_a"));
            c.setCodigo_a(rs.getInt("codigo_a"));
            return c;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public List<Area> getAreasActivos() throws Exception {
        try {
            AreaControlador ac = new AreaControlador();
            return ac.getAreasActivos();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
